package com.example.lib5.hashTab.test.testF;

/**
 * 创建日期：2022/5/31 0031 10:54
 *
 * @author tony.sun
 * 类说明：
 */
public class HashTabDemoF {
    public static void main(String[] args) {
        HashTab hashTab = new HashTab(7);
        hashTab.add(new RoleNote(1, "唐三"));
        hashTab.add(new RoleNote(2, "小舞"));
        hashTab.add(new RoleNote(3, "千仞雪"));
        hashTab.add(new RoleNote(4, "比比东"));
        hashTab.add(new RoleNote(11, "比比东2"));
        hashTab.add(new RoleNote(5, "唐昊"));
        hashTab.list();
        hashTab.search(11);
    }
}

//节点
class RoleNote {
    public int no;
    public String name;
    public RoleNote next;

    public RoleNote(int no, String name) {
        this.no = no;
        this.name = name;
    }

    @Override
    public String toString() {
        return "RoleNote{" +
                "no=" + no +
                ", name='" + name + '\'' +
                '}';
    }
}

//链表
class LinkedList {
    RoleNote head;

    public void add(RoleNote role) {
        if (head == null) {
            head = role;
            return;
        }
        RoleNote temp = head;
        while (true) {
            if (temp.next == null) {
                break;
            }
            temp = temp.next;
        }
        temp.next = role;
    }

    public void list(int i) {
        //判断链表是否为空
        if (head == null) {
            System.out.println(i+"链表为空");
            return;
        }
        //循环后移打印链表
        RoleNote temp=head;
        while (true) {
            System.out.println(i+temp.toString());
            if (temp.next==null) {
                break;
            }
            temp=temp.next;
        }
    }

    public RoleNote searchByNo(int no) {
        //判断节点是否为空
        if (head==null) {
            return null;
        }
        //开始死循环查询
        RoleNote temp=head;
        boolean isFind=false;
        while (true) {
            if (temp.no==no) {
                isFind=true;
                break;
            }
            if (temp.next==null) {
                isFind=false;
                break;
            }
            temp=temp.next;
        }
        return isFind==true?temp:null;
    }
}

//哈希表
class HashTab {
    private int size;
    LinkedList[] linkedLists;

    public HashTab(int size) {
        this.size = size;
        linkedLists = new LinkedList[size];
        for (int i = 0; i < linkedLists.length; i++) {
            linkedLists[i] = new LinkedList();
        }
    }

    public void add(RoleNote role) {
        //获取第几个数组里面的呢
        int value = gethashTabSize(role.no);
        LinkedList linkedList = linkedLists[value];
        linkedList.add(role);
    }

    private int gethashTabSize(int no) {
        return no % size;
    }

    public void list() {
        for (int i = 0; i < linkedLists.length; i++) {
            linkedLists[i].list(i);
        }
    }

    public void search(int no) {
        int i = gethashTabSize(no);
        LinkedList linkedList = linkedLists[i];
        RoleNote roleNote = linkedList.searchByNo(no);
        if (roleNote==null) {
            System.out.println("没有");
        }else {
            System.out.println(i+roleNote.toString());
        }

    }
}

